import request from "../request.js"

const base_url = 'https://api.wxls.pro/study' || 'http://192.168.1.139:9000' || '' || 'https://api.wxls.pro/cms'

let header = {
	"content-type": "application/json"
}

/**
 * 获得个人中心
 */
export const get_personal = async (par,wxlsToken) => {
	let header = { "content-type":"application/json" }
	if(wxlsToken && wxlsToken.wxlsToken){
		header['Authorization'] = `bearer ${wxlsToken.wxlsToken}`
	}
	let { data } = await request({
		url: `${base_url}/api/military`,
		header,
		method: 'GET',
		data: par
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}

/**
 * 学院绑定学号
 */
export const bing_NameAndNo = async (par,wxlsToken) => {
	let header = { "content-type":"application/json" }
	if(wxlsToken && wxlsToken.wxlsToken){
		header['Authorization'] = `bearer ${wxlsToken.wxlsToken}`
	}
	let { data } = await request({
		url: `${base_url}/api/military/bindingStudentNum2`,
		header,
		method: 'GET',
		data: par
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}

/**
 * 学生绑定CID
 */
export const bing_StuCid = async (par,wxlsToken) => {
	let header = { "content-type":"application/json" }
	if(wxlsToken && wxlsToken.wxlsToken){
		header['Authorization'] = `bearer ${wxlsToken.wxlsToken}`
	}
	let { data } = await request({
		url: `${base_url}/api/military/updateCid`,
		header,
		method: 'GET',
		data: par
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}

/**
 * 设置积分
 */
export const set_points = async (par,wxlsToken) => {
	let header = { "content-type":"application/json" }
	if(wxlsToken && wxlsToken.wxlsToken){
		header['Authorization'] = `bearer ${wxlsToken.wxlsToken}`
	}
	let { data } = await request({
		url: `${base_url}/api/military/getIntegral`,
		header,
		method: 'GET',
		data: par
	});
	if(data.code === '3001'){
		return
	}
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}

// 获取今日已获得任务积分
export const get_today_points = async (wxlsToken) => {
	let header = { "content-type":"application/json" }
	if(wxlsToken && wxlsToken.wxlsToken){
		header['Authorization'] = `bearer ${wxlsToken.wxlsToken}`
	}
	let { data } = await request({
		url: `${base_url}/api/military/dayIntegral`,
		header,
		method: 'GET'
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}